package com.colorfruit.admin.control.controller;

import com.alibaba.fastjson.JSON;
import com.colorfruit.admin.business.service.PayBusinessOrderService;
import com.colorfruit.admin.business.service.ShopCartService;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.vo.payorder.GeneratePayVo;
import com.colorfruit.admin.pojo.vo.payorder.PayBusinessOrderItemVo;
import com.colorfruit.admin.pojo.vo.payorder.PayBusinessOrderVo;
import com.colorfruit.admin.pojo.vo.shopcard.ShopCartVo;
import com.colorfruit.framework.model.pojo.BaseResult;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shopCart")
public class ShopCartController {

    @Autowired
    HttpServletRequest request ;

    @Autowired
    ShopCartService shopCartService;

    @Autowired
    PayBusinessOrderService payBusinessOrderService;

    @GetMapping("/addToShopCart")
    public BaseResult addToShopCart( Long preShelfOrderId, Integer buyNum ){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] addToShopCart param: userId = {}, preShelfOrderId = {}, buyNum = {}", userId, preShelfOrderId, buyNum);
            BaseResult baseResult = shopCartService.addToShopCart(userId, preShelfOrderId, buyNum);
            log.info("# [OUT] [controller] addToShopCart SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/selectShopCartList")
    public BaseResult selectShopCartList( String productName, int page, int size ){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] selectShopCartList param: userId = {}, productName = {}, page = {}, size = {} ", userId, productName, page, size);
            List<ShopCartVo> shopCartVos = shopCartService.selectShopCartList(userId, productName, page, size);
            log.info("# [OUT] [controller] selectShopCartList SUCCESS");
            return new BaseResult(shopCartVos);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/selectHistoryShopCartList")
    public BaseResult selectHistoryShopCartList( String productName, int page, int size ){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] selectHistoryShopCartList param: userId = {}, productName = {}, page = {}, size = {} ", userId, productName, page, size);
            PageInfo<ShopCartVo> shopCartVoPageInfo = shopCartService.selectHistoryShopCartList(userId, productName, page, size);
            log.info("# [OUT] [controller] selectHistoryShopCartList SUCCESS");
            return new BaseResult(shopCartVoPageInfo);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }



    @GetMapping("/deleteProduct")
    public BaseResult deleteProduct( String shoppingCardIds){
        String userId = request.getHeader("userId");
        log.info("# [IN] [controller] deleteProduct param: userId = {}, shoppingCardIds = {} ", userId, shoppingCardIds);
        shopCartService.deleteProduct(userId, shoppingCardIds);
        log.info("# [OUT] [controller] deleteProduct SUCCESS");
        return new BaseResult(ErrorCodeEnum.SUCCESS);
    }

    @GetMapping("/generatePayOrder")
    public BaseResult generatePayOrder( String  generatePayVoListString ){
        try {
            List<GeneratePayVo> generatePayVoList = JSON.parseArray(generatePayVoListString, GeneratePayVo.class) ;
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller]generatePayOrder param: userId = {},  generatePayVoListString = {}, generatePayVoList = {} ", userId, generatePayVoListString, generatePayVoList.size());
            for (GeneratePayVo generatePayVo : generatePayVoList){
                log.info("payBusinessOrderItemVo = {}", generatePayVo.toString());
            }
            BaseResult baseResult = payBusinessOrderService.generatePayOrder(userId, generatePayVoList);
            log.info("# [OUT] [controller] generatePayOrder SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/getPayOrder")
    public BaseResult getPayOrder(String businessNo){
        log.info("# [IN] [controller] getPayOrder param: businessNo = {} ", businessNo);
        String userId = request.getHeader("userId");
        BaseResult payOrder = payBusinessOrderService.getPayOrder(userId, businessNo);
        log.info("# [OUT] [controller] getPayOrder SUCCESS");
        return payOrder;
    }

    @GetMapping("/editPayOrder")
    public BaseResult editPayOrder(String payBusinessOrderVoString, String payBusinessOrderItemVoListString){
        try {
            String userId = request.getHeader("userId");
            log.info("# [IN] [controller] generatePayOrder param: payBusinessOrderVo = {}, payBusinessOrderItemVoListString = {} ", payBusinessOrderVoString, payBusinessOrderItemVoListString);
            PayBusinessOrderVo payBusinessOrderVo = JSON.parseObject(payBusinessOrderVoString, PayBusinessOrderVo.class);
            List<PayBusinessOrderItemVo> payBusinessOrderItemVoList = JSON.parseArray(payBusinessOrderItemVoListString, PayBusinessOrderItemVo.class);
            String remoteHost = request.getRemoteHost();
            for (PayBusinessOrderItemVo payBusinessOrderItemVo : payBusinessOrderItemVoList){
                log.info("payBusinessOrderItemVo = {}", payBusinessOrderItemVo.toString());
            }
            BaseResult baseResult = payBusinessOrderService.editPayOrder(userId, remoteHost, payBusinessOrderVo, payBusinessOrderItemVoList);
            log.info("# [OUT] [controller] generatePayOrder SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }



}
